package com.qk.interceptor;



import com.qk.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;



@Slf4j
@Component
public class TokenInterceptor  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*String token = request.getRequestURI();

        if(token.contains("login")){
            log.info("用户登录: {} " , token);

            return  true;
        }
*/
        String jwt = request.getHeader("token");

        if(jwt == null || jwt.isEmpty()){
            //log.info("用户未登录: {} " , token);
            log.info("请求头token不存在");
            response.setStatus(401);
            return false;
        }

        try{
            JwtUtils.parseToken(jwt);
        }catch (Exception e){
          //  log.info("令牌解析失败: {} " , token);
            log.info("令牌解析失败");
            response.setStatus(401);
            return  false;
        }
        log.info("令牌解析成功, 放行");
        return  true;
    }

}
